package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.kungfu.comm.AccessFlag;
import com.cndw.kungfu.comm.JXSpreadTask;
import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsDailyItem;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsFlag;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsQueue;
import com.cndw.kungfu.config.ConstantsTask;
import com.cndw.kungfu.config.DiscipleConfig;
import com.cndw.kungfu.config.GoodsEffectConfig;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.LevelUpgrade;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.domain.Queue;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.PropertiesLib;
import com.cndw.kungfu.ext.StringProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.ext.WordProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.disciple.DiscipleDower;
import com.cndw.kungfu.model.disciple.DiscipleFeature;
import com.cndw.kungfu.model.disciple.DiscipleManager;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.disciple.domain.DiscipleCompararor;
import com.cndw.kungfu.model.disciple.domain.DiscipleRein;
import com.cndw.kungfu.model.goods.PlayerBoxs;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.DiscipleSmallVO;
import com.cndw.kungfu.vo.DiscipleVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 门派徒弟控制器
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-9-5 下午12:14:43
 */
@Controller
public class DiscipleController {
	@Autowired
	private DiscipleManager discipleManager;

	/**
	 * 徒弟的通用配置
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage showConfig(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("discipleSkill", DiscipleDower.getDowers());
		map.put("discipleFeature", DiscipleFeature.getFeatures());
		map.put("discipleMax", player.getDescipleMax());
		map.put("devilGold", DiscipleConfig.devilGold);
		map.put("devilAdd", DiscipleConfig.devilAdd);
		map.put("devilAsset", DiscipleConfig.devilAsset);

		map.put("featureRefreshGold", DiscipleConfig.featureRefreshGold);
		map.put("featureUpgreade", DiscipleConfig.featureUpgreade);
		map.put("drnTimes", player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.drn));
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 显示我的所有徒弟
	 * 
	 * @param session
	 * @return List<DiscipleVO>
	 */
	public ResponseMessage showDisciples(Session session, boolean isCamp) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Collection<Disciple> disciples = playerMartial.getDisciples();
		List<DiscipleVO> discipleVOs = new ArrayList<DiscipleVO>();
		for (Disciple e : disciples) {
			if (isCamp && e.getCampId() == 0) {
				continue;
			}
			discipleVOs.add(e.toVO());
		}
		int extGrid = 0;
		if (player.getLevel() >= ConstantsLevel.lv70DiscipleGrid
				&& player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp) >= DiscipleConfig.discipleGridExpMax) {
			extGrid = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("disciples", discipleVOs);
		map.put("isCamp", isCamp);
		map.put("campNum", player.getCampNum() + extGrid);
		map.put("autoTrain", player.getDataInfo().intDataGet(ConstantsIntKey.autoTrain));
		map.put("expPool", player.getExpPool());
		map.put("gridExp", player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp));

		QQDC.getInstance().logClick(player, LogFieldType.disciple);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 单个显示
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage showOne(Session session, int id) {
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		return ResponseMessage.getSuccMessage(disciple.toVO());
	}

	public ResponseMessage showOther(Session session, long playerId) {
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player == null) {
			return null;
		}
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		int score = 0;

		List<DiscipleSmallVO> vos = new ArrayList<DiscipleSmallVO>();
		ArrayList<Disciple> list = new ArrayList<Disciple>(playerMartial.getDisciples());
		Collections.sort(list, new DiscipleCompararor());

		for (Disciple e : list) {
			if (e.getCampId() <= 0) {
				continue;
			}
			score += e.getScore();

			vos.add(e.toSmallVO());
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("smallDisciples", vos);
		map.put("score", score);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 显示当前我的酒馆
	 * 
	 * @param session
	 * @param type
	 * @return
	 */
	public ResponseMessage reinShow(Session session) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		Map<String, Object> map = new HashMap<String, Object>();
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);

		int tavernLastRefresh = player.getDataInfo().intDataGet(ConstantsIntKey.tavernTime);
		int goodsId = GoodsEffectConfig.getDiscipleReinGoodsId();

		map.put("goodsNum", player.getPlayerBoxs().getStackNum(goodsId));
		map.put("tavernLastRefresh", tavernLastRefresh);
		map.put("reinList", playerMartial.getBufferReins());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 执行刷新
	 * 
	 * @param session
	 * @param type
	 *            1 擂台比武
	 * @return
	 */
	public ResponseMessage reinRefresh(Session session, int type, int num) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		if (num != 1 && num != 10) {
			return null;
		}
		String goodsName = null;
		if (type == 0) {
			int tavernLastRefresh = player.getDataInfo().intDataGet(ConstantsIntKey.tavernTime);
			if (tavernLastRefresh + 900 > TimeProcesser.getUnixTime()) {
				return null;
			}
			player.getDataInfo().intDataSet(ConstantsIntKey.tavernTime, TimeProcesser.getUnixTime());
			num = 10;
		} else {
			PlayerBoxs playerBoxs = player.getPlayerBoxs();
			int goodsId = GoodsEffectConfig.getDiscipleReinGoodsId();
			GoodsMode goodsMode = Context.getGoodsServer().getModeById(goodsId);
			if (playerBoxs.getStackNum(goodsId) < num) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.goods"));
			}
			playerBoxs.costGoods(goodsId, num);
			goodsName = goodsMode.getName();
			//TODO1 添加招聘积分 想要加时间限制 记录清理 10月23日上线
			//player.getDataInfo().intDataAlter(ConstantsIntKey.trd, num);
		}

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < num; i++) {
			DiscipleRein discipleRein = discipleManager.makeRefresh(type);
			playerMartial.reinBuffer(discipleRein);
			sb.append(discipleRein.getName()).append(discipleRein.getQuality()).append(",");
		}
		if (type == 0) {
			LogServer.recordLog(player, ConstantsLog.disciple, 80001, sb.toString());

		} else {
			player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.buyPercentItem);
			LogServer.recordLog(player, ConstantsLog.disciple, 80012, goodsName, num, sb.toString());
		}
		player.getDataInfo().commitSync();

		return reinShow(session);
	}

	/**
	 * 收服徒弟
	 * 
	 * @param session
	 * @param discipleId
	 * @return
	 */
	public ResponseMessage discipleRein(Session session, String indexIdstr) {
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		int[] indexIds = StringProcesser.toIntArray(indexIdstr);
		int canReinNum = player.getDescipleMax() - playerMartial.getDisciples().size();
		if (indexIds == null || indexIds.length == 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}

		if (player.getLevel() < ConstantsLevel.lv30Rein) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.disciplerein.level", ConstantsLevel.lv30Rein));
		}

		if (canReinNum <= 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.disciplerein.reachmaxnum"));
		} else if (canReinNum < indexIds.length) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.disciplerein.cannum", canReinNum));
		}
		List<DiscipleRein> reinList = playerMartial.getBufferReins();
		List<DiscipleVO> vos = new ArrayList<DiscipleVO>();
		List<DiscipleRein> reins = new ArrayList<DiscipleRein>();
		for (int indexId : indexIds) {
			if (indexId >= reinList.size()) {
				continue;
			}
			DiscipleRein rein = reinList.get(indexId);
			if (rein == null) {
				continue;
			}
			reins.add(rein);
			Disciple disciple = discipleManager.createDisciple(playerId, rein);
			int id = discipleManager.insertDisciple(disciple);
			if (id > 0) {
				disciple.setId(id);
				playerMartial.addDisciple(disciple);
				vos.add(disciple.toVO());

				// 添加“门派-收服徒弟”日志
				LogServer.recordLog(player, ConstantsLog.disciple, 80013, id, disciple.getName(), disciple.getQuality());

				if (disciple.getQuality() >= 2) {
					if (disciple.getQuality() >= 3) {
						Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark24Rein3, 1);
					}

					// ChatMain.systemMessage(LanguageLib.get("disciple.reen.broadcast",
					// player.getPlayerName(), disciple.getName(),
					// LangUtil.getColor(disciple.getQuality())));
					ChatMain.systemMessage(LanguageLib.get("disciple.reen.broadcast", LangUtil.makeNameLink(player), disciple.getName(),
							LangUtil.getColor(disciple.getQuality())));
				}
			}
		}
		reinList.removeAll(reins);// 串行架构,不考虑并发.所以后删除.若底层变动,需调整
		/**
		 * 招募了一个徒弟
		 */
		Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark8DiscipleRein, 1);
		Context.getGeneralServer().addTilte(player.getPlayerId(), Constants.titleDiscipleRein, 0);
		JXSpreadTask.complete(player, JXSpreadTask.reinDis);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("reinDiciples", vos);
		map.put("reinList", reinList);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 删除徒弟
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage giveUp(Session session, int id) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		if (player.getDataInfo().isGoodsLock()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("player.pass.lockctrl"));
		}
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		playerMartial.delDisciple(disciple.getId());
		disciple.setPlayerId(0);
		disciple.commitSync();

		// 添加“门派-删除徒弟”日志
		LogServer.recordLog(player, ConstantsLog.disciple, 80014, disciple.getName(), disciple.getId());

		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			return ResponseMessage.getSuccMessage(player.toPlayerVO());
		}
		return ResponseMessage.getSuccMessage(true);
	}

	/**
	 * 放入助阵营
	 * 
	 * @param session
	 * @param id
	 * @param campId
	 *            传0取消助阵营
	 * @return 位子不够,已有id均不返回数据.设置成功返回true
	 */
	public ResponseMessage campDisciple(Session session, int id, int campId) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		if (campId <= 0) {
			disciple.setCampId(0);
		} else {
			if (disciple.getCampId() > 0) {
				return null;
			}
			Collection<Disciple> disciples = playerMartial.getDisciples();
			int haveCampIdNums = 0;
			int canCamp = player.getCampNum();
			if (player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp) >= DiscipleConfig.discipleGridExpMax) {
				canCamp++;
			}
			for (Disciple e : disciples) {
				if (e.getCampId() == campId) {
					return null;
				}
				if (e.getCampId() > 0) {
					haveCampIdNums++;
				}
			}

			if (haveCampIdNums >= canCamp) {
				return null;
			} else if (campId > canCamp) {
				return null;
			}
			disciple.setCampId(campId);
		}
		disciple.commitSync();
		Context.getPlayerServer().clacProperties(player);
		player.commitSync();

		return ResponseMessage.getSuccMessage(player.toPlayerVO());
	}

	/**
	 * 重修
	 * 
	 * @param session
	 * @param discipleId
	 * @return
	 */
	public ResponseMessage practicePercent(Session session, int id) {
		long playerId = session.getUid();
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player.getGoldAll() < DiscipleConfig.disciplePracticeGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}

		if (player.getLevel() < ConstantsLevel.lv17Practice) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.practice.level", ConstantsLevel.lv17Practice));
		}

		player.alterGoldAll(-DiscipleConfig.disciplePracticeGold);
		discipleManager.rateDisciplePercent(disciple);

		disciple.commitSync();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("disciple", disciple.toVO());
		data.put("gold", player.getGold());
		data.put("goldBing", player.getGoldBing());
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			data.put("player", player.toPlayerVO());
		}

		// player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.trainAgainPercentItem);

		// 添加“门徒-徒弟重修”日志
		LogServer.recordLog(player, ConstantsLog.disciple, 80015, disciple.getName(), disciple.getId(), DiscipleConfig.disciplePracticeGold);
		LogServer.costEventLog(playerId, DiscipleConfig.disciplePracticeGold, ConstantsCost.disciplePractice);

		return ResponseMessage.ticpMsg(LanguageLib.get("disciple.practice.suc"), data);
	}

	/**
	 * 特训
	 * 
	 * @param session
	 * @param discipleId
	 * @return
	 */
	public ResponseMessage devilTrain(Session session, int id, boolean useGold) {
		long playerId = session.getUid();
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}

		Player player = Context.getPlayerServer().getByPlayerId(playerId);

		if (player.getLevel() < ConstantsLevel.lv26DevilTrain) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.devilTrain.level", ConstantsLevel.lv26DevilTrain));
		}
		int devilTimes = disciple.getDevilTimes();
		int maxDevilTimes = disciple.getGrowth() >= 1300 ? DiscipleConfig.devilMaxTimes : disciple.getGrowth() >= 1200 ? 18 : 15;// 徒弟成长值小于1200点时，只能特训15次,1200-1300
																																	// 17次
		if (devilTimes >= maxDevilTimes) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.devilTrain.max"));
		}
		int costGold = 0, costAsset = 0;
		if (useGold) {
			costGold = DiscipleConfig.devilGold[devilTimes];
		} else {
			costAsset = DiscipleConfig.devilAsset[devilTimes];
		}

		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		if (player.getAsset() < costAsset) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}
		if (costGold > 0) {
			player.alterGoldAll(-costGold);
			LogServer.costEventLog(playerId, costGold, ConstantsCost.discipleTrain);
		} else {
			player.alterAsset(-costAsset);
		}

		disciple.setDevilTimes(devilTimes + 1);// 记录特训次数
		disciple.clacPropertys();
		disciple.commitSync();
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("disciple", disciple.toVO());
		data.put("asset", player.getAsset());
		data.put("gold", player.getGold());
		data.put("goldBing", player.getGoldBing());

		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			data.put("player", player.toPlayerVO());
		}

		Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark11DisclipleDevilTrain, 1);
		player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.trainPercentItem);
		// 添加“门派-徒弟特训”日志
		LogServer.recordLog(player, ConstantsLog.disciple, 80016, disciple.getName(), disciple.getId(), costGold, disciple.getDevilTimes(), costAsset);

		return ResponseMessage.ticpMsg(LanguageLib.get("disciple.devilTrain.suc"), data);

	}

	/**
	 * 修行
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage trainDisciples(Session session, int id, boolean autoMax) {
		long playerId = session.getUid();
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		if (disciple.getLevel() >= player.getLevel()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.traindisciples.nomoretrain"));
		}

		if (player.getExpPool() + disciple.getExp() < disciple.getExpNeed()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.train.noexp"));
		}
		Map<String, Object> map = new HashMap<String, Object>();
		long costAll = 0;
		do {
			long alter = disciple.getExpNeed() - disciple.getExp();
			costAll += alter;
			disciple.alterExp(alter);
			player.alterExpPool(-alter);

		} while (autoMax && disciple.getLevel() < player.getLevel() && disciple.getExpNeed() <= player.getExpPool());

		/**
		 * 修行徒弟
		 */
		Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark10DisclipleTrain, 1);

		disciple.clacScore();

		disciple.commitSync();
		player.commitSync();

		map.put("disciple", disciple.toVO());
		map.put("expPool", player.getExpPool());

		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}
		// 添加“门派-徒弟修行”日志
		LogServer.recordLog(player, ConstantsLog.disciple, 80017, disciple.getName(), disciple.getId(), costAll);
		String msg = LanguageLib.get("disciple.train.suc", disciple.getName(), disciple.getLevel(), costAll);

		return ResponseMessage.ticpMsg(msg, map);
	}

	/**
	 * 是否自动助阵营的徒弟
	 * 
	 * @param session
	 * @param isOk
	 * @return
	 */
	public ResponseMessage aotoTrainOnUpgrade(Session session, boolean isOk) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (isOk) {
			player.getDataInfo().intDataSet(ConstantsIntKey.autoTrain, 1);
		} else {
			player.getDataInfo().intDataRemove(ConstantsIntKey.autoTrain);
		}
		LogServer.recordLog(player, ConstantsLog.disciple, 80022, isOk);

		return ResponseMessage.getSuccMessage(isOk);
	}

	/**
	 * 徒弟洗髓
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage washing(Session session, int id, int qualified) {
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		qualified = qualified < 0 || qualified > 4 ? 0 : qualified;
		int costGold = DiscipleConfig.discipleWashingGold;
		if (qualified > 0) {
			costGold = DiscipleConfig.discipleWashingSafeGold;
		}
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		String oldQualified = disciple.getDiscipleMode().getName();
		int qualifiedType = 0;
		do {
			qualifiedType = NumberProcesser.mtRand(1, 4);// 资质类型

		} while (qualifiedType == disciple.getDiscipleMode().getQualifiedType());

		if (qualified > 0) {
			qualifiedType = qualified;// 智能洗髓 洗到此方向
		}

		int discipleId = disciple.getQuality() * 100 + qualifiedType + 100;
		disciple.setDiscipleId(discipleId);
		discipleManager.rateDisciplePercent(disciple);

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("disciple", disciple.toVO());
		data.put("gold", player.getGold());
		data.put("goldBing", player.getGoldBing());
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			data.put("player", player.toPlayerVO());
		}
		player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.xs, qualified > 0 ? 4 : 1);

		disciple.commitSync();
		player.commitSync();
		player.getDataInfo().getEveryDay().finishDailyItem(ConstantsDailyItem.washPercentItem);
		LogServer.recordLog(player, ConstantsLog.disciple, 80021, disciple.getName(), disciple.getId(), oldQualified, disciple.getDiscipleMode().getName(),
				costGold);
		LogServer.costEventLog(player.getPlayerId(), costGold, ConstantsCost.discipleWashing);
		String msg = LanguageLib.get("disciple.washing.suc");
		return ResponseMessage.ticpMsg(msg, data);
	}

	public ResponseMessage washingGoods(Session session, int id, int qualified) {
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		if (disciple.getDiscipleMode().getQualifiedType() != qualified) {
			return null;
		}
		if (!player.getPlayerBoxs().isHaveGoods(GoodsEffectConfig.washingId)) {
			return null;
		}
		int after = 0;
		int[] floatArr = disciple.getDiscipleMode().getQualifiedFloatArr();
		switch (qualified) {
		case 1:
			if (disciple.getPowerPercent() < 290 || disciple.getPowerPercent() >= floatArr[1]) {
				return null;
			}
			disciple.setPowerPercent(disciple.getPowerPercent() + 1);
			after = disciple.getPowerPercent();
			break;
		case 2:
			if (disciple.getPhysicalPercent() < 290 || disciple.getPhysicalPercent() >= floatArr[3]) {
				return null;
			}
			disciple.setPhysicalPercent(disciple.getPhysicalPercent() + 1);
			after = disciple.getPhysicalPercent();
			break;
		case 3:
			if (disciple.getAgilePercent() < 290 || disciple.getAgilePercent() >= floatArr[5]) {
				return null;
			}
			disciple.setAgilePercent(disciple.getAgilePercent() + 1);
			after = disciple.getAgilePercent();
			break;
		case 4:
			if (disciple.getWitPercent() < 290 || disciple.getWitPercent() >= floatArr[7]) {
				return null;
			}
			disciple.setWitPercent(disciple.getWitPercent() + 1);
			after = disciple.getWitPercent();
			break;

		default:
			break;
		}
		player.getPlayerBoxs().costGoods(GoodsEffectConfig.washingId, 1);
		disciple.clacPropertys();
		disciple.commitSync();
		
		LogServer.recordLog(player, ConstantsLog.disciple, 80033, id, 1, player.getPlayerBoxs().getStackNum(GoodsEffectConfig.washingId), qualified, 1, after,disciple.getName());
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("disciple", disciple.toVO());
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			data.put("player", player.toPlayerVO());
		}
		return ResponseMessage.getSuccMessage(data);
	}

	/**
	 * 学习新的天赋
	 * 
	 * @param session
	 * @param id
	 *            徒弟id
	 * @param bookId
	 *            技能书id
	 * @param oldDowerId
	 *            被替换的天赋id,默认0
	 * @return
	 */
	public ResponseMessage studyDower(Session session, int id, int bookId, int oldDowerId) {
		long playerId = session.getUid();
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		GoodsMode goodsMode = Context.getGoodsServer().getModeById(bookId);
		if (goodsMode == null || goodsMode.getAppId() != 151) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player.getPlayerBoxs().getStackNum(bookId) < 1) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.goods"));
		}
		int dowerType = goodsMode.getValueA();
		int dowerLevel = goodsMode.getValueB();

		if (disciple.getLevel() < goodsMode.getPlayerLevel()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.dower.needlevel", goodsMode.getPlayerLevel()));
		}

		DiscipleDower dower = DiscipleDower.getById(dowerType);
		if (dower == null || dowerLevel <= 0 || dowerLevel > dower.getAdd().length) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}

		Map<Integer, Integer> dowerMap = disciple.getDowerMap();
		if (oldDowerId > 0 && dowerMap.containsKey(oldDowerId) == false) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.args"));
		}
		if (dowerLevel > 1) {
			if (dowerMap.containsKey(dowerType) == false || dowerMap.get(dowerType) + 1 < dowerLevel) {
				// return
				// ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.dower.needfront"));
			}
		}
		if (dowerMap.containsKey(dowerType)) {
			if (dowerMap.get(dowerType) >= dowerLevel) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.dower.errlevel"));
			} else if (oldDowerId == 0) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
			} else if (oldDowerId != dowerType) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
			}
		} else {
			int maxSize = 3;
			if (oldDowerId == 0 && dowerMap.size() >= maxSize) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.dower.needchoose"));
			}
		}

		player.getPlayerBoxs().costGoods(bookId, 1);
		if (oldDowerId > 0) {
			disciple.getDowerMap().remove(oldDowerId);
		}
		disciple.getDowerMap().put(dowerType, dowerLevel);
		disciple.clacPropertys();
		disciple.commitSync();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("disciple", disciple.toVO());
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}

		// 添加“门派-学习天赋”日志
		LogServer.recordLog(player, ConstantsLog.disciple, 80018, disciple.getName(), disciple.getId(), goodsMode.getName(), dower.getName(), dowerLevel);

		return ResponseMessage.ticpMsg(LanguageLib.get("disciple.dower.suc", disciple.getName(), goodsMode.getName()), map);
	}

	/**
	 * 天赋刷新
	 * 
	 * @param session
	 * @param id
	 * @param type
	 * @return
	 */
	public ResponseMessage featureRefresh(Session session, int id, int type) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		int oldFeature = disciple.getFeatureType();
		int oldlevel = disciple.getFeatureLevel();
		int costGold = type == 1 ? DiscipleConfig.featureRefreshGold[0] : DiscipleConfig.featureRefreshGold[1];
		if (costGold > player.getGoldAll()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.commitSync();
		int featrueNew = NumberProcesser.mtRand(8);
		int levelNew = NumberProcesser.rateIndex(DiscipleConfig.featrueRefreshArr, 1000) + 1;

		if (type == 1) {
			disciple.setFeatureType(featrueNew);
			disciple.setFeatureLevel(levelNew);
			disciple.clacPropertys();
			disciple.commitSync();
			disciple.setFeatrueTmp(null);
		} else {
			disciple.setFeatrueTmp(new int[] { featrueNew, levelNew });
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("featrueNew", featrueNew);
		map.put("levelNew", levelNew);
		if (type == 1) {
			map.put("disciple", disciple.toVO());
		}
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}
		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.featureRefresh);
		DiscipleFeature featureOld = DiscipleFeature.getById(oldFeature);
		DiscipleFeature featureNew = DiscipleFeature.getById(featrueNew);

		LogServer.recordLog(player, ConstantsLog.disciple, 80024, disciple.getName(), featureOld != null ? featureOld.getName() : null, oldlevel,
				featureNew.getName(), levelNew, type, costGold);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 天赋选择
	 * 
	 * @param session
	 * @param id
	 * @param ok
	 * @return
	 */
	public ResponseMessage featureChoice(Session session, int id, boolean ok) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(id);

		if (disciple == null || disciple.getFeatrueTmp() == null || disciple.getFeatrueTmp().length != 2) {
			return null;
		}
		int featrueNew = disciple.getFeatrueTmp()[0];
		int levelNew = disciple.getFeatrueTmp()[1];
		Map<String, Object> map = new HashMap<String, Object>();
		if (ok) {
			disciple.setFeatureType(featrueNew);
			disciple.setFeatureLevel(levelNew);
			disciple.clacPropertys();
			disciple.commitSync();
			if (disciple.getCampId() > 0) {
				Context.getPlayerServer().clacProperties(player);
				map.put("player", player.toPlayerVO());
			}
		}
		disciple.setFeatrueTmp(null);
		DiscipleFeature featureNew = DiscipleFeature.getById(featrueNew);
		LogServer.recordLog(player, ConstantsLog.disciple, 80025, disciple.getName(), featureNew.getName(), levelNew, ok ? 1 : 0);

		map.put("disciple", disciple.toVO());
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 天赋升级
	 * 
	 * @param session
	 * @param id
	 * @return
	 */
	public ResponseMessage featureUpgrade(Session session, int id) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null || disciple.getFeatureLevel() >= 4) {
			return null;
		}
		int costGold = DiscipleConfig.featureUpgreade[disciple.getFeatureLevel() - 1];
		if (costGold > player.getGoldAll()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.commitSync();
		int featureLevel = disciple.getFeatureLevel() + 1;
		disciple.setFeatureLevel(featureLevel);
		disciple.clacPropertys();
		disciple.commitSync();
		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.featureUpgrade);
		DiscipleFeature featureNew = DiscipleFeature.getById(disciple.getFeatureType());
		LogServer.recordLog(player, ConstantsLog.disciple, 80026, disciple.getName(), featureNew.getName(), featureLevel, costGold);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("disciple", disciple.toVO());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}

		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage gestShow(Session session) {
		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		Queue queue = null;
		if (queues != null && !queues.isEmpty()) {
			Iterator<Queue> it = queues.iterator();
			while (it.hasNext()) {
				Queue queue2 = (Queue) it.next();
				if (queue2.getType() == ConstantsQueue.typeOnGest) {
					queue = queue2;
					break;
				}
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("endTime", queue == null ? 0 : queue.getEndTime());
		map.put("id", queue == null ? 0 : queue.getParam1());
		map.put("gestAdd", DiscipleConfig.gestAdd);
		map.put("gestCost", DiscipleConfig.gestCost);
		map.put("fastAmt", ConstantsModel.accelerateSecondOfGold);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 徒弟学习
	 * 
	 * @param session
	 * @param id
	 * @param getstId
	 * @return
	 */
	@AccessFlag(ConstantsFlag.gest)
	public ResponseMessage gestStudy(Session session, int id, int getstId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}
		if (getstId != disciple.getGestSign() + 1) {
			return null;
		} else if (getstId > 81) {
			return null;
		}
		int indexId = NumberProcesser.ceil(getstId, 9) - 1;
		int deamId = NumberProcesser.floor(player.getDataInfo().intDataGet(ConstantsIntKey.dreamId) - 1000, 9);
		if (deamId <= indexId) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.gest.dream", LangUtil.numFormat(indexId + 1)));
		}
		int levelNeed = 50 + indexId * 5;
		if (disciple.getLevel() < levelNeed) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.gest.level", levelNeed, disciple.getLevel()));
		}

		int costExpJar = DiscipleConfig.gestCost[indexId][0];
		int costAsset = DiscipleConfig.gestCost[indexId][1];
		int costSconds = DiscipleConfig.gestCost[indexId][2];
		if (player.getExpJar() < costExpJar) {
			return null;
		} else if (player.getAsset() < costAsset) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}

		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		if (queues != null && !queues.isEmpty()) {
			Iterator<Queue> it = queues.iterator();
			while (it.hasNext()) {
				if (it.next().getType() == ConstantsQueue.typeOnGest) {
					return ResponseMessage.ticpErrMsg(LanguageLib.get("skill.study.havestrength"));
				}
			}
		}

		player.alterAsset(-costAsset);
		player.alterExpJar(-costExpJar);
		player.commitSync();

		int beginTime = TimeProcesser.getUnixTime();
		int endTime = beginTime + costSconds;
		Queue queue = new Queue();
		queue.setPlayerId(session.getUid());
		queue.setType(ConstantsQueue.typeOnGest);
		queue.setFuncType(ConstantsQueue.funcTypeGestStudy);
		queue.setTitle(disciple.getName());
		queue.setBeginTime(beginTime);
		queue.setEndTime(endTime);
		queue.setParam1(id);
		queue.setParam2(getstId);
		queue.setNextExecTime(endTime);
		Context.getQueueServer().insertQueue(queue);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("asset", player.getAsset());
		map.put("expJar", player.getExpJar());
		map.put("endTime", endTime);
		LogServer.recordLog(player, ConstantsLog.disciple, 80027, disciple.getName(), disciple.getId(), costAsset, costExpJar, player.getExpJar(), getstId);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 学习加速
	 * 
	 * @param session
	 * @return
	 */
	@AccessFlag(ConstantsFlag.gest)
	public ResponseMessage gestFastComplete(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		List<Queue> queues = Context.getQueueServer().getQueueIds(session.getUid(), false);
		if (queues == null || queues.isEmpty()) {
			return null;
		}
		Queue queue = null;
		Iterator<Queue> it = queues.iterator();
		while (it.hasNext()) {
			Queue tmpQueue = it.next();
			if (tmpQueue.getType() == ConstantsQueue.typeOnGest) {
				queue = tmpQueue;
				break;
			}
		}
		if (queue == null) {
			return null;
		}
		int laveTime = queue.getEndTime() - TimeProcesser.getUnixTime();
		if (laveTime + 3 <= 0) {// 还有3秒不能加速
			return null;
		}
		int costGold = NumberProcesser.ceil(laveTime, ConstantsModel.accelerateSecondOfGold);
		if (player.getKnightLevel() == 4) {

			costGold = 0;
		}
		if (costGold < 0) {
			return null;
		}
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}

		player.alterGoldAll(-costGold);
		queue.setEndTime(TimeProcesser.getUnixTime());
		Context.getQueueServer().immediately(queue);
	
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(queue.getParam1());
		if (costGold > 0) {
			LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.gestFast);
			LogServer.recordLog(player, ConstantsLog.disciple, 80028, disciple.getName(), disciple.getId(), costGold);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("costGold", costGold);
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("player", player.toPlayerVO());
		map.put("disciple", disciple.toVO());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 徒弟洗成长值
	 * 
	 * @param session
	 * @param discipleId
	 * @param type
	 *            0普通 1白金 2至尊
	 * @return
	 */
	public ResponseMessage washPoint(Session session, int discipleId, int type, boolean useBaoFu) {
		if (type < 0 || type > 2) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(discipleId);
		if (disciple == null) {
			return null;
		}

		if (disciple.getGrowth() >= disciple.getDiscipleMode().getGrowthMax()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.growth.max"));
		}
		
		if (useBaoFu && !player.getPlayerBoxs().isHaveGoods(GoodsEffectConfig.disBaoFuIds[type])) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.goods"));
		}
		
		int cost = useBaoFu ? 0 : DiscipleConfig.washCost[type];

		if (!useBaoFu && type == 0 && player.getAsset() < cost) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}

		if (!useBaoFu && type > 0 && player.getGoldAll() < cost) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}

		if (useBaoFu) {
			player.getPlayerBoxs().costGoods(GoodsEffectConfig.disBaoFuIds[type], 1);
		} else if (type > 0) {
			player.alterGoldAll(-cost);
			LogServer.costEventLog(session.getUid(), cost, ConstantsCost.washPoint);
		} else {
			player.alterAsset(-cost);// 普通洗点
		}  

		Map<String, Object> map = new HashMap<String, Object>();
		int point = NumberProcesser.rateIndex(DiscipleConfig.rateWash[type], 1000);
		if (point > 0) {
			if ((disciple.getGrowth() + point) > disciple.getDiscipleMode().getGrowthMax()) {
				point = disciple.getDiscipleMode().getGrowthMax() - disciple.getGrowth();
				disciple.setGrowth(disciple.getDiscipleMode().getGrowthMax());
			} else {
				disciple.setGrowth(disciple.getGrowth() + point);
			}
			disciple.clacPropertys();
			disciple.commitSync();
			if (disciple.getCampId() > 0) {
				Context.getPlayerServer().clacProperties(player);
				map.put("player", player.toPlayerVO());
			}
			map.put("disciple", disciple.toVO());
			if (point >= 3) {
				ChatMain.systemMessage(LanguageLib.get(point == 3 ? "disciple.growth.crit1" : "disciple.growth.crit2", player.getPlayerName(),
						LangUtil.makeHtml(disciple.getQuality(), disciple.getName()), point));
			}
		}
		player.commitSync();
		//记录徒弟洗点积分
		//player.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.dp, DiscipleConfig.washScore[type]);
		
		//TODO 记录徒弟洗点次数  结束：11-20
		String key = ConstantsDayIntKey.dwp0;
		if (type > 0) {
			key = type == 1 ? ConstantsDayIntKey.dwp1 : ConstantsDayIntKey.dwp2;
		}
		player.getDataInfo().getEveryDay().intDataAlter(key, 1);
		player.getDataInfo().commitSync();
		
		LogServer.recordLog(player, ConstantsLog.disciple, 80029, type, useBaoFu, type == 0 ? 0 : cost, type == 0 ? cost : 0,
				disciple.getName(), disciple.getId(), point, disciple.getGrowth());

		map.put("point", point);
		map.put("asset", player.getAsset());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 改徒弟名字
	 * 
	 * @param session
	 * @param id
	 * @param name
	 * @return
	 */
	public ResponseMessage rename(Session session, int id, String name) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple disciple = playerMartial.getDiscipleById(id);
		if (disciple == null) {
			return null;
		}

		if (disciple.getQuality() > 2) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.cant"));
		}

		int altTimes = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.drn);
		if (altTimes > 0) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.times"));
		}
		name = name.trim();
		String[] names1 = PropertiesLib.getCacheArr(PropertiesLib.nameXml, "discipleSpecial3");// 紫色徒弟名字
		String[] names2 = PropertiesLib.getCacheArr(PropertiesLib.nameXml, "discipleSpecial4");// 橙色徒弟名字
		for (int i = 0; i < names1.length; i++) {
			if (name.equals(names1[i])) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.duplicate"));
			}
		}
		for (int i = 0; i < names2.length; i++) {
			if (name.equals(names2[i])) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.duplicate"));
			}
		}

		int nameLength = name.getBytes(Constants.charset).length;
		if (nameLength < 4 || nameLength > 15) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.length", 4, 15));
		} else if (!WordProcesser.chkPerg(name, ConstantsConfig.getCommCreateNameExp())) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.err"));
		} else if (WordProcesser.chkBadWorlds(name) != null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.name.err"));
		}
		String oldName = disciple.getName();
		disciple.setName(name);
		disciple.commitSync();
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.drn, altTimes + 1);
		player.getDataInfo().commitSync();
		LogServer.recordLog(player, ConstantsLog.disciple, 80030, oldName, disciple.getName());
		return ResponseMessage.getSuccMessage(disciple.getName());
	}

	/**
	 * 开放徒弟槽
	 * 
	 * @param session
	 * @param useGold
	 * @return
	 */
	public ResponseMessage openGrid(Session session, boolean useGold) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv70DiscipleGrid) {
			return null;
		}
		int currentExp = player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp);
		if (currentExp >= DiscipleConfig.discipleGridExpMax) {
			return null;
		}
		if (useGold) {
			if (player.getGoldAll() < DiscipleConfig.discipleGridGold) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
			}
		} else {
			if (player.getAsset() < DiscipleConfig.discipleGridAsset) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
			}
		}

		boolean suc = false, bigCrit = false;
		int exp = 0;
		int costGold = 0, costAsset = 0;
		if (useGold) {
			suc = true;
			bigCrit = NumberProcesser.isWinning(10);
			exp = bigCrit ? 40 : 4;
			costGold = DiscipleConfig.discipleGridGold;
			player.alterGoldAll(-costGold);
			LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.discipleGridOpen);
		} else {
			suc = NumberProcesser.isWinning(20);
			exp = suc ? 2 : 0;
			costAsset = DiscipleConfig.discipleGridAsset;
			player.alterAsset(-costAsset);
		}
		player.commitSync();

		if (exp > 0) {
			player.getDataInfo().intDataSet(ConstantsIntKey.dGridExp, NumberProcesser.between(currentExp + exp, 0, DiscipleConfig.discipleGridExpMax));
			player.getDataInfo().commitSync();
		}
		LogServer.recordLog(player, ConstantsLog.disciple, 80031, costAsset, costGold, exp, player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp));

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("asset", player.getAsset());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("expGet", exp);
		map.put("suc", suc);
		map.put("bigCrit", bigCrit);
		map.put("gridExp", player.getDataInfo().intDataGet(ConstantsIntKey.dGridExp));

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 显示周游天下面板
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage tourShow(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());

		int tUse = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.tUse);
		int tl = player.getDataInfo().intDataGet(ConstantsIntKey.tl);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tUse", tUse);
		map.put("tl", tl);
		map.put("dragonGold", DiscipleConfig.tourDragonCost);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 周游天下
	 * 
	 * @param session
	 * @param discipleId
	 * @param type
	 *            0一骑绝尘 1御剑飞行 2御龙在天
	 * @return
	 */
	@AccessFlag(ConstantsFlag.discTour)
	public ResponseMessage tour(Session session, int discipleId, int type) {
		if (type < 0 || type > 2) {
			return null;
		}
		long playerId = session.getUid();
		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		if (playerMartial == null) {
			return null;
		}
		Disciple disciple = playerMartial.getDiscipleById(discipleId);
		if (disciple == null) {
			return null;
		}
		if (disciple.getLevel() >= player.getLevel()) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.tour.lvover"));
		}
		if (disciple.getLevel() < DiscipleConfig.lv60Tour) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.tour.level"));
		}

		int tUse = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.tUse);
		int tAssetTimes = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.tat);

		if (type != 2 && tUse >= DiscipleConfig.tourPhysical) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.tour.useout"));
		}
		// if (type == 3 &&
		// player.getDataInfo().intDataGet(ConstantsIntKey.payCash) < 100000) {
		// return null;
		// }

		int curLocation = player.getDataInfo().intDataGet(ConstantsIntKey.tl);
		if (curLocation >= DiscipleConfig.tourMax) {
			curLocation = 0;
		}
		boolean free = false;
		if (type == 0) {
			free = tAssetTimes < 2;
		}
		if (type == 0 && !free && player.getAsset() < DiscipleConfig.tourCost[type]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		} else if (type == 1 && player.getGoldAll() < DiscipleConfig.tourCost[type]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		} else if (type == 2 && player.getGoldAll() < DiscipleConfig.tourDragonCost[disciple.getLevel() - DiscipleConfig.lv60Tour]) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		int oldLv = disciple.getLevel();
		long oldExp = disciple.getExp();
		int costAsset = 0;
		int costGold = 0;
		boolean roundEnd = false;
		Map<String, Object> map = new HashMap<String, Object>();
		if (type < 2) {
			player.getDataInfo().getEveryDay().intDataAlter(type == 0 ? ConstantsDayIntKey.tat : ConstantsDayIntKey.tgt, 1);
			if (!free) {
				if (type == 0) {
					costAsset = DiscipleConfig.tourCost[type];
				} else {
					costGold = DiscipleConfig.tourCost[type];
				}
			}
			int result = NumberProcesser.rateIndex(DiscipleConfig.tourLvRate[type][curLocation], 100);// 0升1停2降

			int tGoldTimes = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.tgt);
			boolean crit = false;
			if (type == 1 && tGoldTimes <= 5) {// 元宝前五次必然暴击
				crit = true;
			} else {
				crit = NumberProcesser.isWinning(DiscipleConfig.tourCritRate[type][curLocation]);
			}

			curLocation += result == 0 ? 1 : result == 2 ? -1 : 0;
			boolean highR = player.getDataInfo().intDataGet(ConstantsIntKey.payCash) > 50000;
			tUse += (crit || (highR && type == 2)) ? 0 : 1;// 暴击或者消费大于50000玩家使用御龙在天时不消耗体力
			int coff = result == 2 ? 1 : crit ? 10 : 1;// 降级单倍，暴击10陪，普通1陪
			long exp = coff * DiscipleConfig.tourExpBase[type][curLocation];
			if (curLocation == DiscipleConfig.tourMax) {// 跑通全图
				int roundEndCoff = type == 1 ? 30 : 20;
				int expExt = DiscipleConfig.tourExpBase[type][4] * roundEndCoff;
				exp += expExt;
				map.put("expExt", expExt);
			}

			disciple.alterExp(exp);

			map.put("gotExp", exp);
			map.put("isCrit", crit);
			map.put("result", result);

		} else if (type == 2) {// 直升一级
			costGold = DiscipleConfig.tourDragonCost[disciple.getLevel() - DiscipleConfig.lv60Tour];
			disciple.setLevel(disciple.getLevel() + 1);
			disciple.clacPropertys();
			curLocation = DiscipleConfig.tourMax;
			roundEnd = true;
		}
		disciple.commitSync();

		if (costAsset > 0) {
			player.alterAsset(-costAsset);
		} else if (costGold > 0) {
			player.alterGoldAll(-costGold);
			LogServer.costEventLog(session.getUid(), costGold, type == 1 ? ConstantsCost.tourCost1 : ConstantsCost.tourCost2);
		}

		if (disciple.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}
		player.commitSync();
		player.getDataInfo().intDataSet(ConstantsIntKey.tl, curLocation);
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.tUse, tUse);
		player.getDataInfo().commitSync();

		LogServer.recordLog(player, ConstantsLog.disciple, 80032, type, discipleId, costAsset, costGold, oldLv, oldExp, disciple.getLevel(), disciple.getExp());

		map.put("disciple", disciple.toVO());
		map.put("asset", player.getAsset());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("tUse", tUse);
		map.put("tl", curLocation);
		map.put("roundEnd", roundEnd);

		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 传承显示
	 * 
	 * @param session
	 * @param sId  主体
	 * @param tId
	 * @return
	 */
	public ResponseMessage heritageShow(Session session, int sId, int tId) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple sDispile = playerMartial.getDiscipleById(sId);
		Disciple tDispile = playerMartial.getDiscipleById(tId);
		if (sDispile == null || tDispile == null) {
			return null;
		}
		long allExp = tDispile.getExp();
		for (int i = 1; i < tDispile.getLevel(); i++) {
			allExp += LevelUpgrade.getByLevel(i).getDiscipleExp();
		}
		
		int level = sDispile.getLevel();
		long expNeed = LevelUpgrade.getByLevel(level).getDiscipleExp() - sDispile.getExp();
		long tmpExp = allExp;
		while (level < player.getLevel() && tmpExp >= expNeed) {
			tmpExp -= expNeed;
			level++;
			expNeed = LevelUpgrade.getByLevel(level).getDiscipleExp();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("expHer", allExp);
		map.put("level", level);
		return ResponseMessage.getSuccMessage(map);
	}
	
	/**
	 * 传承
	 * 
	 * @param session
	 * @param sId 主体
	 * @param tId
	 * @param type  1元宝，2银两
	 * @param herType  1 经验传承 2 特训传承
	 * @return
	 */
	public ResponseMessage heritage(Session session, int sId, int tId, int type, int herType) {
		if (type < 1 || type > 2 || herType < 1 || herType > 2) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		PlayerMartial playerMartial = Context.getDiscipleServer().getByPlayerId(session.getUid());
		Disciple sDispile = playerMartial.getDiscipleById(sId);
		Disciple tDispile = playerMartial.getDiscipleById(tId);
		if (sDispile == null || tDispile == null) {
			return null;
		}

		boolean isCostGold = type == 1;
		int cost = 0;
		if (herType == 1) {
			if (sDispile.getLevel() >= player.getLevel()) {//徒弟等级和人物等级相等就不让继承了
				return null;
			}
			
			int indexLv = 0;
			for (int i = 0; i < ConstantsModel.petHeritageCost.length; i++) {
				if (tDispile.getLevel() <= DiscipleConfig.heritageExpCost[i][0]) {
					indexLv = i;
					break;
				}
			}
			cost = DiscipleConfig.heritageExpCost[indexLv][type]; 
		} else if (herType == 2){
			if (tDispile.getQuality() > sDispile.getQuality() || tDispile.getDevilTimes() <= sDispile.getDevilTimes()) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.heritage.condition"));
			}
			//进行了16次及以上次数特训的徒弟，不能把特训属性传承给成长值低于1200点的徒弟
			if (tDispile.getDevilTimes() >= 16 && sDispile.getGrowth() < 1200) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.heritage.condition"));
			}
			if (tDispile.getDevilTimes() >= 19 && sDispile.getGrowth() < 1300) {
				return ResponseMessage.ticpErrMsg(LanguageLib.get("disciple.heritage.condition"));
			}
			cost = DiscipleConfig.heritageDevilCost[tDispile.getDevilTimes() - 1][type];
		}
		
		if (isCostGold && player.getGoldAll() < cost) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		} else if (!isCostGold && player.getAsset() < cost) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}
		int sBefor = 0;
		int tBefor = 0;
		long expHeritage = 0;
		if (herType == 1) {//经验传承
			long allExp = tDispile.getExp();
			for (int i = 1; i < tDispile.getLevel(); i++) {
				allExp += LevelUpgrade.getByLevel(i).getDiscipleExp();
			}
			if (allExp <= 0) {
				return null;
			}
			sBefor = sDispile.getLevel();
			tBefor = tDispile.getLevel();
			int level = sBefor;
			long expNeed = LevelUpgrade.getByLevel(level).getDiscipleExp() - sDispile.getExp();
			long expLeave = allExp; 
			while (level < player.getLevel() && expLeave >= expNeed) {
				expLeave -= expNeed;
				level++;
				expNeed = LevelUpgrade.getByLevel(level).getDiscipleExp();
			}
			
			if (expLeave > 0 && expLeave < expNeed) {//升到人物等级，剩余经验不足升级
				expLeave = 0;
			} 
			expHeritage = allExp - expLeave;
			sDispile.alterExp(expHeritage);
			sDispile.commitSync();
			
			tDispile.setLevel(1);
			tDispile.setExp(0);
			if (expLeave > 0) {
				tDispile.alterExp(expLeave);
			}
			tDispile.clacPropertys();
			tDispile.commitSync();
			
		} else if (herType == 2) {
			sBefor = sDispile.getDevilTimes();
			tBefor = tDispile.getDevilTimes();
			
			sDispile.setDevilTimes(tBefor);
			sDispile.clacPropertys();
			sDispile.commitSync();
			
			tDispile.setDevilTimes(sBefor);
			if (sBefor == 0) {
				tDispile.setPhysicalAdd(0);
				tDispile.setPowerAdd(0);
				tDispile.setAgileAdd(0);
				tDispile.setWitAdd(0);
			}
			tDispile.clacPropertys();
			tDispile.commitSync();
		}
		
		if (isCostGold) {
			player.alterGoldAll(-cost);
			LogServer.costEventLog(session.getUid(), cost, ConstantsCost.disHeritage);
		} else {
			player.alterAsset(-cost);
		}
		player.commitSync();
		LogServer.recordLog(player, ConstantsLog.disciple, 80034, herType, isCostGold ? cost : 0, isCostGold ? 0 : cost, sDispile.getId(), tDispile.getId(), 
				expHeritage, herType == 1 ? sBefor : sDispile.getLevel(), sDispile.getLevel(), herType == 1 ? tBefor : tDispile.getLevel(), tDispile.getLevel(), herType == 2 ? sBefor : sDispile.getDevilTimes(), 
						 sDispile.getDevilTimes(), herType == 2 ? tBefor : tDispile.getDevilTimes(), tDispile.getDevilTimes(),sDispile.getName(),tDispile.getName());
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (sDispile.getCampId() > 0 || tDispile.getCampId() > 0) {
			Context.getPlayerServer().clacProperties(player);
			map.put("player", player.toPlayerVO());
		}
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("asset", player.getAsset());
		map.put("sVO", sDispile.toVO());
		map.put("tVO", tDispile.toVO());
		return ResponseMessage.getSuccMessage(map);
	}

}
